package com.zyark.web.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName: CheckUtils  <br/>
 * Function:  <br/>
 * Reason:  <br/>
 * Date: 2017-05-24 10:29 <br/>
 *
 * @author Ku_ker
 * @version 1.0
 * @JDK 1.7
 */
public class CheckUtils {

    /**
     * 校验该字符串不是为空或者不是空格
     *
     * @param msg
     *
     * @return
     */
    public static boolean isNotNullAndBlank(String msg) {

        return (isNotNull(msg) && !msg.trim().equals(""));
    }

    /**
     * 判断对象不为空
     *
     * @param object
     *
     * @return
     */
    public static boolean isNotNull(Object object) {
        return object != null;
    }

    /**
     * isMobileNO作用: 手机号验证 . <br/>
     * 适用条件：.<br/>
     *
     * @param mobiles
     *         手机号码
     *
     * @return
     *
     * @author Ku_ker
     * @since JDK 1.7
     */
    public static boolean isMobileNO(String mobiles) {

        if (!isNotNullAndBlank(mobiles)) {
            return false;
        }

        Pattern p = null;
        Matcher m = null;
        p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 判断该字符串是否为身份证
     *
     * @param cardid
     *
     * @return
     */
    public static boolean isIdCard(String cardid) {

        if (!isNotNullAndBlank(cardid)) {
            return false;
        }
        if (cardid.length() != 18) {
            return false;
        }
        char[] l_id = cardid.toCharArray();
        int l_jyw = 0;
        int[] wi = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
        char[] ai = new char[]{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        for (int i = 0; i < 17; i++) {
            if (l_id[i] < '0' || l_id[i] > '9') {
                return false;
            }
            l_jyw += (l_id[i] - '0') * wi[i];
        }
        l_jyw = l_jyw % 11;
        if (ai[l_jyw] != l_id[17]) {
            return false;
        }
        return true;
    }

}
